const path = require("path");
const { merge } = require("webpack-merge");
const webpack = require("webpack");
const LoadablePlugin = require("@loadable/webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const getBaseWebpackConfig = require("./webpack.config.base");
const paths = require("./paths");
// const WatchMissingNodeModulesPlugin = require("react-dev-utils/WatchMissingNodeModulesPlugin");
// const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const isEnvProduction = process.env.NODE_ENV === "production";
const isEnvDevelopment = process.env.NODE_ENV === "development";

const webpackConfig = merge(getBaseWebpackConfig(false), {
	entry: {
		app: [isEnvDevelopment && require.resolve("webpack-hot-middleware/client"), "./src/entry-client"].filter(Boolean),
	},
	output: {
		filename: isEnvProduction ? "public/js/[name].[chunkhash].js" : "public/js/[name].[hash].js",
	},
	externals: isEnvProduction
		? {
				axios: "axios",
				react: "React",
				"react-dom": "ReactDOM",
				"react-router": "ReactRouter",
				"react-router-dom": "ReactRouterDOM",
		  }
		: {},
	optimization: {
		// webpack引导模块
		runtimeChunk: {
			name: "manifest",
		},
	},
	plugins: [
		new LoadablePlugin({
			filename: "client-manifest.json",
			writeToDisk: true,
		}),
		new HtmlWebpackPlugin({
			inject: true,
			template: "public/index.html",
			filename: "public/index.html",
			packTime: +new Date(),
			minify: {
				removeComments: true,
				collapseWhitespace: true,
				removeRedundantAttributes: true,
				useShortDoctype: true,
				removeEmptyAttributes: true,
				removeStyleLinkTypeAttributes: true,
				keepClosingSlash: true,
				minifyJS: true,
				minifyCSS: true,
				minifyURLs: true,
			},
		}),
		// new ForkTsCheckerWebpackPlugin({
		// 	async: !isEnvProduction,
		// 	typescript: {
		// 		profile: true,
		// 		mode: "write-references",
		// 		diagnosticOptions: {
		// 			syntactic: true,
		// 			semantic: true,
		// 			declaration: true,
		// 			global: true,
		// 		},
		// 		configFile: paths.appTsConfig,
		// 	},
		// 	logger: {
		// 		infrastructure: "console",
		// 		issues: "console",
		// 		devServer: false,
		// 	},
		// }),
		isEnvDevelopment && new webpack.HotModuleReplacementPlugin(),
		// isEnvDevelopment && new WatchMissingNodeModulesPlugin(paths.appNodeModules),
	].filter(Boolean),
});

module.exports = webpackConfig;
